package com.naiterui.ehp.bs.patient.repository;

import com.naiterui.common.repository.core.BaseJpaRepository;
import com.naiterui.ehp.bp.domain.ConsultSession;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.List;

/**
 * <pre>
 * 问诊会话持久化接口实现
 * </pre>
 *
 * @author fangguanhong
 * @date 2020/4/9 下午4:54
 * @since 1.0.0
 */
@Repository
public interface IApConsultSessionRepository extends BaseJpaRepository<ConsultSession, Long> {

    @Query("select c from ConsultSession c where c.endTime is not null and c.patientId = ?1 order by c.startTime desc")
    Page<ConsultSession> findByPatientId(Long patientId, Pageable pageable);

    ConsultSession findFirstByBillId(Long billId);

    ConsultSession findFirstByPatientIdAndDoctorIdOrderByStartTimeDesc(Long patientId, Long doctorId);

    ConsultSession findFirstBySessionId(String sessionId);

    long countByDoctorIdAndEndTimeIsNotNull(Long doctorId);

    List<ConsultSession> findByDoctorIdIn(Collection<Long> doctorIds);

    ConsultSession findFirstByPatientIdAndDoctorIdOrderByEndTimeDesc(Long patientId, Long doctorId);

    ConsultSession findFirstByPatientIdAndDoctorIdAndEndTimeIsNull(Long patientId, Long doctorId);
}
